﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.MassSlaughter;
using System.Text.Json;
using System.Security.Policy;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;

namespace Butcher.winform.EnterpriseForms.MessSalguhterPut
{
    public partial class WeighingScaleShow : Form
    {
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        private float X, Y;
        private void setTag(Control cons)//Control类，定义控件的基类
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;//获取或设置包含有关控件的数据的对象
                if (con.Controls.Count > 0)
                    setTag(con);//递归算法
            }
        }
        private void setControls(float newx, float newy, Control cons)//实现控件以及字体的缩放
        {
            foreach (Control con in cons.Controls)
            {
                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);//递归
                }
            }
        }


        private void MyForm_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / X;//当前宽度与变化前宽度之比
            float newy = this.Height / Y;//当前高度与变化前宽度之比
            setControls(newx, newy, this);
            this.Text = this.Width.ToString() + " " + this.Height.ToString();  //窗体标题显示长度和宽度
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Resize += new EventHandler(MyForm_Resize);
            X = this.Width;
            Y = this.Height;
            setTag(this);
        }


        Scale ScaleInfo = new Scale();
        public WeighingScaleShow()
        {
            InitializeComponent();
            label11.Text = Convert.ToString(DateTime.Now.ToString("yyyy-MM-dd"));
            label11.ForeColor = Color.White;
            InitializeDataGridView();
            uiTextBox2.LostFocus += new EventHandler(textBox1_GotFocus);
            getList();
            var url = @"http://localhost:5229/api/Batch/WhiteBulkList";

            HttpClient client = new HttpClient();
            NEWPUT nEWPUT = new NEWPUT();
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                uiTextBox3.Text = ScaleInfo.TareWeightDeduction.ToString();
                getStash(ScaleInfo.Warehouse);
            }
            var content = new StringContent(JsonSerializer.Serialize<NEWPUT>(nEWPUT));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var count = info["date"].Value<JArray>();
            List<BuclkWeight> enterprises = count.ToObject<List<BuclkWeight>>();

            ProductWeighing k = new ProductWeighing();

            var l = Convert.ToInt32(k.h);

            UIFlowLayoutPanel uIFlowLayoutPanel = new UIFlowLayoutPanel();


            uIFlowLayoutPanel.Width = 240;
            uIFlowLayoutPanel.Height = 150;
            uIFlowLayoutPanel.BackColor = Color.Blue;

            for (int i = 0; i < enterprises.Count; i++)
            {
                UIButton newgetbutton = new UIButton();

                newgetbutton.Location = new Point(10, 10);
                newgetbutton.Click += new EventHandler(MyButton_Click);
                newgetbutton.Text = "选择" + "" + i + "";
                newgetbutton.Name = i.ToString();


                Label newlabel = new Label();
                Label newlabe4 = new Label();

                newlabe4.Width = 150;
                newlabel.Width = 200;

                newlabel.Text = "批宰计划";
                newlabe4.Text = enterprises[i].SlaughterNumber;


                uIFlowLayoutPanel.Controls.Add(newlabel);
                uIFlowLayoutPanel.Controls.Add(newlabe4);
                uIFlowLayoutPanel.Controls.Add(newgetbutton);


            }

            uiFlowLayoutPanel2.Controls.Add(uIFlowLayoutPanel);

        }
        public void textBox1_GotFocus(object sender, EventArgs e)
        {
            uiTextBox4.Text = (Convert.ToDecimal(uiTextBox2.Text) - Convert.ToDecimal(uiTextBox3.Text)).ToString();
        }
        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }
        public static int getid;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {






            var l = e.ColumnIndex;




            if (l == 1)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                var id = row.Cells["序号"].Value;

                string url = @"http://localhost:5229/api/Batch/ReturnWeighingDel";
                HttpClient client = new HttpClient();
                hkss list = new hkss();
                list.ByproductId = Convert.ToInt32(id);

                var content = new StringContent(JsonSerializer.Serialize<hkss>(list), Encoding.UTF8);
                content.Headers.Remove("Content-Type");
                content.Headers.Add("Content-Type", "application/json");
                string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                JObject josnresult = JObject.Parse(result);
                var results = josnresult["code"];

                if (results != null)
                {
                    MessageBox.Show("删除成功");
                    GEtheml();
                }
            }
            else if (l == 0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                var id = row.Cells["序号"].Value;

                getid = Convert.ToInt32(id);
                WeighingScaleShowUpdata list = new WeighingScaleShowUpdata();
                list.Show();


            }

        }
        private void InitializeDataGridView()
        {
            DataGridViewButtonColumn editColumn = new DataGridViewButtonColumn();
            editColumn.HeaderText = "操作";
            editColumn.Text = "修改";
            editColumn.UseColumnTextForButtonValue = true;

            DataGridViewButtonColumn editColumn1 = new DataGridViewButtonColumn();
            editColumn1.HeaderText = "操作";
            editColumn1.Text = "删除";
            editColumn1.UseColumnTextForButtonValue = true;

            dataGridView1.Columns.Add(editColumn);
            dataGridView1.Columns.Add(editColumn1);
            dataGridView1.CellClick += dataGridView1_CellContentClick;
        }





        public void GEtheml()
        {


            string url = @"http://localhost:5229/api/Batch/ReturnWeighingHtml";
            HttpClient client = new HttpClient();
            hks list = new hks();

            var content = new StringContent(JsonSerializer.Serialize<hks>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"].Value<JArray>();

            Button lists = new Button();




            dataGridView1.DataSource = results;
        }

        public class hks
        {


        }



        public class hkss
        {

            public int ByproductId { get; set; }

        }

        private void button12_Click(object sender, EventArgs e)
        {
            Byproduct byproduct = new Byproduct();
            byproduct.Stashid = Convert.ToInt32(cb_Level.SelectedValue);
            byproduct.DocumentsNo = "123123A";
            byproduct.ByproductDate = DateTime.Now;
            byproduct.ShoppingType = "猪肝";
            byproduct.ShoppingName = "猪肝";
            byproduct.byproductNum = Convert.ToInt32(uiTextBox5.Text);
            byproduct.byproductNoteWeight = Convert.ToDecimal(uiTextBox2.Text);
            byproduct.byproductNoteTare = Convert.ToDecimal(uiTextBox3.Text);
            byproduct.byproductNetWeight = Convert.ToDecimal(uiTextBox2.Text) - Convert.ToDecimal(uiTextBox3.Text);
            byproduct.Rremark = uiTextBox1.Text;
            string url = @"http://localhost:5229/api/Batch/ReturnWeighingCreate";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Byproduct>(byproduct));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
        }

        public void getList()
        {
            string url = @"http://localhost:5229/api/Batch/getWhiteListDTo";
            HttpClient client = new HttpClient();
            WhiteListDTo whiteListDTo = new WhiteListDTo();
            var content = new StringContent(JsonSerializer.Serialize<WhiteListDTo>(whiteListDTo));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["date"].Value<JArray>();
            dataGridView1.DataSource = aaa;


        }

        private void uiFlowLayoutPanel2_Click(object sender, EventArgs e)
        {

        }

        private void MyButton_Click(object sender, EventArgs e)
        {
            UIButton button = sender as UIButton;
            int gh = 0;
            if (button != null)
            {
                gh = Convert.ToInt32(button.Name);
            }
            var url = @"http://localhost:5229/api/Batch/WhiteBulkList";

            HttpClient client = new HttpClient();
            NEWPUT nEWPUT = new NEWPUT();

            var content = new StringContent(JsonSerializer.Serialize<NEWPUT>(nEWPUT));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var count = info["date"].Value<JArray>();
            List<BuclkWeight> enterprises = count.ToObject<List<BuclkWeight>>();




            var hk = 0;
            for (var i = 0; i < enterprises.Count; i++)
            {
                hk = Convert.ToInt32(enterprises[gh].SlaughterNumber);



            }

            MessageBox.Show("ok" + hk);

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            
        }

        private void uiButton1_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            ScaleBackfill.pid = 4;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Owner = this;
            scale.Hide();
            scale.ShowDialog();
            Application.ExitThread();
        }
    }
}
